package com.hjx.movie.core.util;

import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>Title:</p>
 * <p>Description:</p>
 *
 * @author hjx
 * @date 2021/1/15 - 0:38
 */
@Component
public class CacheKeyGenerator implements KeyGenerator {
    @Override
    public Object generate(Object target, Method method, Object... params) {
        return target.getClass().getName() +":"+ method.getName() +":"+
                Stream.of(params).map(
                        item->{
                            if (item!=null){
                                return item instanceof Collection?String.valueOf(item.hashCode()):item.toString();
                            }else{
                                return "null";
                            }
                        }).collect(Collectors.joining(","));
    }
}
